package com.seari.service.Impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seari.bean.ImgProgress;
import com.seari.bean.QueryProBean;
import com.seari.mapper.ImgProgressMapper;
import com.seari.pojo.ProRatePojo;
import com.seari.service.ImgProgressService;

@Service
public class ImgProgressServiceImpl implements ImgProgressService {

	@Autowired
	private ImgProgressMapper imgProgressMapper;

	public List<ImgProgress> imgProgressInit(ImgProgress imgProgress) {
		imgProgress.setStart((imgProgress.getPage() - 1)
				* imgProgress.getLimit());
		List<ImgProgress> imgProgresses = imgProgressMapper
				.imgProgressInit(imgProgress);
		return imgProgresses;
	}

	@Override
	public int imgProgressNumCount(ImgProgress imgProgress) {
		int count = imgProgressMapper.imgProgressNumCount(imgProgress);
		return count;
	}

	@Override
	public Map<String, List<String>> queryCapDeviationByProid(String proId) {
		Map<String, List<String>> result = new HashMap<>();
		result.put("deviation",
				imgProgressMapper.queryCapDeviationByProid(proId));
		result.put("remark", imgProgressMapper.queryCapRemarkByProid(proId));
		return result;
	}

	@Override
	public Map<String, List<String>> queryInvDeviationByProid(String proId) {
		Map<String, List<String>> result = new HashMap<>();
		result.put("deviation",
				imgProgressMapper.queryInvDeviationByProid(proId));
		result.put("measures", imgProgressMapper.queryInvMeasuresByProid(proId));
		return result;
	}

	@Override
	public List<ProRatePojo> queryRateConByProid(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		// 分页
		queryProBean.setStart((queryProBean.getStart() - 1)* queryProBean.getLimit());

		List<ProRatePojo> resultData = new ArrayList<ProRatePojo>();
		List<ProRatePojo> queryRateConByProid = imgProgressMapper
				.queryRateConByProid(queryProBean);

		if (queryProBean.getStatu().equals("0")) {
			return queryRateConByProid;
		} else if (queryProBean.getStatu().equals("1")) {
			for (int i = 0; i < queryRateConByProid.size(); i++) {
				if (queryRateConByProid.get(i).getRatio() >= 100) {
					resultData.add(queryRateConByProid.get(i));
				}
			}
			return resultData;
		} else if (queryProBean.getStatu().equals("2")) {

			for (int i = 0; i < queryRateConByProid.size(); i++) {
				if (queryRateConByProid.get(i).getRatio() > 0) {
					resultData.add(queryRateConByProid.get(i));
				}
			}
			return resultData;

		} else if (queryProBean.getStatu().equals("3")) {
			for (int i = 0; i < queryRateConByProid.size(); i++) {
				if (queryRateConByProid.get(i).getRatio() <= 0) {
					resultData.add(queryRateConByProid.get(i));
				}
			}
			return resultData;
		}

		return imgProgressMapper.queryRateConByProid(queryProBean);
	}

	@Override
	public List<ProRatePojo> queryConRateByConid(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		List<ProRatePojo>proRate = imgProgressMapper.queryTemplateByConid(queryProBean);
		List<ProRatePojo> resultData = new ArrayList<ProRatePojo>();
		for (int i = 0; i < proRate.size(); i++) {
			String conTemplateId = proRate.get(i).getConTemplateId();
			
			Double price = imgProgressMapper.getPriceByConTemplateId(conTemplateId);
			Double money = imgProgressMapper.getMoneyByConTemplateId(conTemplateId);
			proRate.get(i).setMoney(money);
			proRate.get(i).setPrice(price);
			if(price > 0) {
			proRate.get(i).setRatio(new BigDecimal(money/price*100).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
			}else {
				proRate.get(i).setRatio(0.0);
			}
			
		}
		if (queryProBean.getStatu().equals("0")) {
			return proRate;
		} else if (queryProBean.getStatu().equals("1")) {
			for (int i = 0; i < proRate.size(); i++) {
				if (proRate.get(i).getRatio() >= 100) {
					resultData.add(proRate.get(i));
				}
			}
			return resultData;
		} else if (queryProBean.getStatu().equals("2")) {

			for (int i = 0; i < proRate.size(); i++) {
				if (proRate.get(i).getRatio() > 0 && proRate.get(i).getRatio() < 100) {
					resultData.add(proRate.get(i));
				}
			}
			return resultData;

		} else if (queryProBean.getStatu().equals("3")) {
			for (int i = 0; i < proRate.size(); i++) {
				if (proRate.get(i).getRatio() <= 0) {
					resultData.add(proRate.get(i));
				}
			}
			return resultData;
		}
		
		return proRate;
	}

	@Override
	public List<ProRatePojo> queryTemplateRateByTemid(QueryProBean queryProBean) {
		List<ProRatePojo> resultData = new ArrayList<ProRatePojo>();
		// TODO Auto-generated method stub
		List<ProRatePojo>proRate = imgProgressMapper.queryTemplateByTemid(queryProBean);
		for (int i = 0; i < proRate.size(); i++) {
			String conTemplateId = proRate.get(i).getConTemplateId();
			
			Double price = imgProgressMapper.getPriceByConTemplateId(conTemplateId);
			Double money = imgProgressMapper.getMoneyByConTemplateId(conTemplateId);
			proRate.get(i).setMoney(money);
			proRate.get(i).setPrice(price);
			if(price > 0) {
			proRate.get(i).setRatio(new BigDecimal(money/price*100).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
			}else {
				proRate.get(i).setRatio(0.0);
			}
		}
		
		if (queryProBean.getStatu().equals("0")) {
			return proRate;
		} else if (queryProBean.getStatu().equals("1")) {
			for (int i = 0; i < proRate.size(); i++) {
				if (proRate.get(i).getRatio() >= 100) {
					resultData.add(proRate.get(i));
				}
			}
			return resultData;
		} else if (queryProBean.getStatu().equals("2")) {

			for (int i = 0; i < proRate.size(); i++) {
				if (proRate.get(i).getRatio() > 0 && proRate.get(i).getRatio() < 100) {
					resultData.add(proRate.get(i));
				}
			}
			return resultData;

		} else if (queryProBean.getStatu().equals("3")) {
			for (int i = 0; i < proRate.size(); i++) {
				if (proRate.get(i).getRatio() <= 0) {
					resultData.add(proRate.get(i));
				}
			}
			return resultData;
		}
		
		return proRate;
	}
}
